package com.qfmy.app.controller.user;

import com.esotericsoftware.minlog.Log;
import com.qfmy.app.service.user.UserService;
import com.qfmy.app.vo.user.UserVo;
import com.qfmy.common.result.Result;
import com.qfmy.model.annotation.LogAnnotation;
import com.qfmy.model.entity.query.LoginQuery;
import com.qfmy.model.entity.vo.CaptchaVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 清风明月
 * @Package com.qfmy.app.controller.user
 * @date 2025/4/13
 * @description 用户信息表
 */
@RestController
@RequestMapping("/app")
@SuppressWarnings("all")
public class UserController {

    /**
     * 注入用户service
     */
    @Resource
    private UserService userService;

    /**
     * 图形验证码接口
     */
    @GetMapping("/user/captcha")
    public Result getCode() {
        CaptchaVo captchaVo = userService.getCode();
        return Result.ok(captchaVo);
    }

    /**
     * 发送短信验证码
     */
    @GetMapping("/user/sms")
    public Result getSmsCode(@RequestParam(name = "phone") String phone) {
        userService.getSmsCode(phone);
        return Result.ok();
    }

    /**
     * 用户登录
     */
    @LogAnnotation(operation = "登录")
    @PostMapping("/user/login")
    public Result login(@RequestBody LoginQuery user) {
        Log.info("用户{}正在登陆",user.getPhone());
        Map<String, String> map = userService.login(user.getPhone(), user.getCode());
        return Result.ok(map);
    }

    /**
     * 用户注册
     */
    @LogAnnotation(operation = "注册")
    @PostMapping("/user/register")
    public Result register(@RequestBody LoginQuery user) {
        userService.register(user);
        return Result.ok();
    }

    /**
     * 修改密码
     */
    @LogAnnotation(operation = "修改密码")
    @PostMapping("/user/updatePassword")
    public Result updatePassword(@RequestBody LoginQuery user) {
        userService.updatePassword(user);
        return Result.ok();
    }

    /**
     * 刷新token
     */
    @GetMapping("/user/refreshToken")
    public Result refreshToken(@RequestParam("refreshToken") String refreshToken) {
        Map<String, String> map = userService.refreshToken(refreshToken);
        return Result.ok(map);
    }

    /**
     * 获取用户信息
     */
    @LogAnnotation(operation = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result<UserVo> getInfo() {
        UserVo userVo = userService.getInfo();
        return Result.ok(userVo);
    }

    /**
     * 退出登录
     */
    @LogAnnotation(operation = "退出登录")
    @GetMapping("/logout")
    public Result logout() {
        userService.logout();
        return Result.ok();
    }

    /**
     * 修改用户信息
     */
    @LogAnnotation(operation = "修改用户信息")
    @PostMapping("/updateInfo")
    public Result updateInfo(@RequestBody UserVo userVo) {
        userService.updateInfo(userVo);
        return Result.ok();
    }

    /**
     * 修改密码
     */
    @GetMapping("/updatePassword")
    public Result updatePassword(@RequestParam String oldPassword, @RequestParam String password) {
        userService.UpdatePassword(oldPassword, password);
        return Result.ok();
    }


}
